package com.kinghood.productcenter.service.async.impl;

import com.kinghood.infrastructure.util.base.IdGenUtil;
import com.kinghood.obs.ObsManger;
import com.kinghood.productcenter.common.model.dao.product.DownloadCenterDO;
import com.kinghood.productcenter.dao.product.ProductBaseMapper;
import com.kinghood.productcenter.service.async.IAsyncBusinessLogService;
import com.kinghood.productcenter.service.async.IAsyncHuaWeiYunService;
import com.kinghood.productcenter.service.product.IDownloadCenterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * 描述:
 * 创建人：jp
 * 创建时间：2024/1/24 14:50
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class AsyncHuaWeiYunServiceImpl implements IAsyncHuaWeiYunService {
    private final String obsFix = "bussiness_center/product_center/product";
    private final ObsManger obsManger;
    private final IDownloadCenterService downloadCenterService;


    @Async("taskExecutor")
    @Override
    public void uploadToHuaWeiYun(Long DLid, String filePath) {
        upload(DLid, filePath);
    }

    @Override
    public void syncuploadToHUaWeiYun(Long DLid, String filePath) {
        upload(DLid, filePath);
    }

    /**
     * 上传数据
     *
     * @param DLid     dlid
     * @param filePath 文件路径
     * @return
     * @author jp
     * @date 2024/01/31 11:55
     */
    private void upload(Long DLid, String filePath) {
        File file = new File(filePath);
//        String huaweiSavePath = obsFix + "/" + IdGenUtil.getId() + file.getName().replaceAll("[\\u4e00-\\u9fa5]", "");
        String huaweiSavePath = obsFix + "/" + file.getName();
        obsManger.fileUpload(filePath, huaweiSavePath);
        downloadCenterService.lambdaUpdate().set(DownloadCenterDO::getRemoteUrl,"/" + huaweiSavePath)
                .set(DownloadCenterDO::getStatus, 1)
                .eq(DownloadCenterDO::getId, DLid)
                .update();
        if (file.exists()) {
            boolean deleted = file.delete();
            log.info("已成功删除文件：" + file.getAbsolutePath() + ", 结果：" + deleted);
        }
    }
}
